package com.meitianhui.convenitenbuy.module.store.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.store.adapter.StoreSearchAdapter;
import com.meitianhui.convenitenbuy.module.store.contract.StoreSearchContract;
import com.meitianhui.convenitenbuy.module.store.di.DaggerStoreComponent;
import com.meitianhui.convenitenbuy.module.store.di.StoreModule;
import com.meitianhui.convenitenbuy.module.store.model.StoreInfo;
import com.meitianhui.convenitenbuy.module.store.presenter.StoreSearchPresenter;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

/*
 Created by amy on 2017/11/29.
 */

public class StoreSearchActivity extends BaseToolbarActivity implements StoreSearchContract.View, BaseQuickAdapter.RequestLoadMoreListener {
    private TextView mTvSearchStoreCancle;
    private EditText mEtSearchStoreInput;
    private LinearLayout mLlSearchStoreNone;
    private RecyclerView mRlvSearchStore;
    @Inject
    StoreSearchPresenter mStoreSearchPresenter;

    List<StoreInfo.Convenient> mSearchStore = new ArrayList<>();
    HashMap<String, String> mDocUrl = new HashMap<>();
    private StoreSearchAdapter mAdapter;

    private int pageIndex = 1;//分页数
    private int pageSize = 20;
    public CharSequence input;

    @Override
    protected int setView() {
        return R.layout.activity_store_search;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerStoreComponent.builder().storeModule(new StoreModule(this)).appComponent(App.getInstance().getAppComponent()).build().inject(this);
        setToolBarGone();
        initId();
        initData();
    }

    private void initData() {
        mAdapter = new StoreSearchAdapter(mSearchStore);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRlvSearchStore.setLayoutManager(manager);
        mRlvSearchStore.setAdapter(mAdapter);
        //上拉加载更多
        mAdapter.setOnLoadMoreListener(this, mRlvSearchStore);
        //获取默认的搜索门店
        getDefualtStore();
        //监听输入框的变化
        mEtSearchStoreInput.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                input = s;
                if (TextUtils.isEmpty(s)) {
                    getDefualtStore();
                } else {
                    getSearchStore(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(StoreSearchActivity.this, StoreDetailActivity.class);
                intent.putExtra("store_id",mSearchStore.get(position).getStores_id());
                intent.putExtra("store_name",mSearchStore.get(position).getStores_name());
                intent.putExtra("store_address",mSearchStore.get(position).getAddress());
                startActivity(intent);
            }
        });
    }

    private void getSearchStore(String s) {
        mStoreSearchPresenter.getSearchStore(s, pageIndex, pageSize);
    }

    private void getDefualtStore() {
        mStoreSearchPresenter.getDefaultStore();
    }

    private void initId() {
        mTvSearchStoreCancle = getViewById(R.id.tv_search_store_cancle);
        mEtSearchStoreInput = getViewById(R.id.et_search_store_input);
        mLlSearchStoreNone = getViewById(R.id.ll_search_store_none);
        mRlvSearchStore = getViewById(R.id.rlv_search_store);

        RxUtils.bindEvents(mTvSearchStoreCancle,this);
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()){
            case R.id.tv_search_store_cancle:
                this.finish();
                break;
        }
    }

    @Override
    protected void onRefreshData() {
        pageIndex = 1;
        if (TextUtils.isEmpty(input)) {
            getDefualtStore();
        } else {
            mStoreSearchPresenter.getSearchStore(input.toString(), pageIndex, pageSize);
        }
    }

    @Override
    public void showError(String msg) {
        TipUtils.showToast(App.getInstance(), msg, 2);
    }

    @Override
    public void getDefaultStoreSucc(StoreInfo storeInfo) {

        mLlSearchStoreNone.setVisibility(View.GONE);
        mRlvSearchStore.setVisibility(View.VISIBLE);

        refreshFinish();
        mSearchStore = storeInfo.getList();
        mDocUrl = storeInfo.getDoc_url();
        mAdapter.setPicUrl(mDocUrl);
        mAdapter.setNewData(mSearchStore);
        mAdapter.setEnableLoadMore(false);
    }

    @Override
    public void showEmpty() {
        mLlSearchStoreNone.setVisibility(View.VISIBLE);
        mRlvSearchStore.setVisibility(View.GONE);
    }

    @Override
    public void getSearchStoreSucc(StoreInfo storeInfo) {

        mLlSearchStoreNone.setVisibility(View.GONE);
        mRlvSearchStore.setVisibility(View.VISIBLE);

        if (pageIndex == 1) {
            mSearchStore.clear();
            mDocUrl.clear();
        }
        mSearchStore.addAll(storeInfo.getList());
        mDocUrl.putAll(storeInfo.getDoc_url());


        mAdapter.setPicUrl(mDocUrl);
        if (mAdapter.isLoading()) {
            mAdapter.loadMoreComplete();
        }
        if (pageIndex == 1) {
            refreshFinish();
            mAdapter.replaceData(storeInfo.getList());
        } else {
            mAdapter.addData(storeInfo.getList());
        }

        mAdapter.setEnableLoadMore(storeInfo.getList().size() < pageSize ? false : true);
    }

    @Override
    public void onLoadMoreRequested() {

        if (TextUtils.isEmpty(input)) {
            return;
        }
        pageIndex++;
        mStoreSearchPresenter.getSearchStore(input.toString(), pageIndex, pageSize);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAdapter.isLoading() && mAdapter != null){
            mAdapter.loadMoreEnd();
        }
        refreshFinish();
    }
}
